<?php

/**
 * Created by PhpStorm.
 * User: leaf
 * Date: 2016/9/20
 * Time: 16:16
 */
class yunleaf extends IController
{  //幻灯片
    function bannerlist()
    {
        $banner = Api::run('getBannerList');
        Yun::ajaxReturn(0, '请求成功', $banner, '');
    }

    //限时抢购
    function getPromotionList()
    {
        $list = [];
        $page = IReq::get('page') ? IFilter::act(IReq::get('page'), 'int') : 1;
        $promitionDB = new IQuery('promotion as p');
        $promitionDB->join = 'left join goods as go on go.id = p.condition';
        $promitionDB->where = 'p.type = 1 and p.is_close = 0 and go.is_del = 0 and NOW() between start_time and end_time AND go.id is not null';
        $promitionDB->fields = 'p.end_time,p.start_time,go.img as img,go.market_price as price,go.name as gname,p.name as name,p.award_value as award_value,go.id as goods_id,p.id as p_id,end_time';
        $promitionDB->order = 'p_id desc';
        $promitionDB->pagesize = '10';
        $promitionDB->page = $page;
        $datalist = $promitionDB->find();
        foreach ($datalist as $data) {
            $data['end_time'] = strtotime($data['end_time']);
            $data['start_time'] = strtotime($data['start_time']);
            $data['now_time'] = time();
            $data['last_time'] = $data['end_time'] - $data['now_time'];
            $list[] = $data;
        }

        $data = array(
            'page' => array(
                'page_total' => $promitionDB->paging->getTotalPage(),
                'page' => $page,
                'page_size' => $promitionDB->pagesize,
                'data_total' => count($promitionDB->find())
            ),
            'datalist' => $list
        );
        Yun::ajaxReturn(0, '请求成功', $data, '');
    }

    //一级目录
    function getCategoryListTop()
    {
        $category = Api::run('getCategoryListTop');
        Yun::ajaxReturn(0, '请求成功', $category, '');
    }

    function products()
    {
        $goods_id = IFilter::act(IReq::get('id'), 'int');

        if (!$goods_id) {
            Yun::ajaxReturn(1, '传递的参数不正确');
        }

        //使用商品id获得商品信息
        $tb_goods = new IModel('goods');
        $goods_info = $tb_goods->getObj('id=' . $goods_id . " AND is_del=0");
        if (!$goods_info) {
            //IError::show(403,"这件商品不存在");
            Yun::ajaxReturn(1, '这件商品不存在');

        }

        //品牌名称
        if ($goods_info['brand_id']) {
            $tb_brand = new IModel('brand');
            $brand_info = $tb_brand->getObj('id=' . $goods_info['brand_id']);
            if ($brand_info) {
                $goods_info['brand'] = $brand_info['name'];
            }
        }

        //获取商品分类
        $categoryObj = new IModel('category_extend as ca,category as c');
        $categoryList = $categoryObj->query('ca.goods_id = ' . $goods_id . ' and ca.category_id = c.id', 'c.id,c.name', 'ca.id desc', 1);
        $categoryRow = null;
        if ($categoryList) {
            $categoryRow = current($categoryList);
        }
        $goods_info['category'] = $categoryRow ? $categoryRow['id'] : 0;

        //商品图片
        $tb_goods_photo = new IQuery('goods_photo_relation as g');
        $tb_goods_photo->fields = 'p.id AS photo_id,p.img ';
        $tb_goods_photo->join = 'left join goods_photo as p on p.id=g.photo_id ';
        $tb_goods_photo->where = ' g.goods_id=' . $goods_id;
        $goods_info['photo'] = $tb_goods_photo->find();

        //商品是否参加促销活动(团购，抢购)
        $goods_info['promo'] = IReq::get('promo') ? IReq::get('promo') : '';
        $goods_info['active_id'] = IReq::get('active_id') ? IFilter::act(IReq::get('active_id'), 'int') : 0;
        if ($goods_info['promo']) {
            $activeObj = new Active($goods_info['promo'], $goods_info['active_id'], $this->user['user_id'], $goods_id);
            //var_dump($activeObj);exit();
            $activeResult = $activeObj->data();
            $activeResult['end_time'] = strtotime($activeResult['end_time']);
            $activeResult['start_time'] = strtotime($activeResult['start_time']);
            $activeResult['now_time'] = time();
            $activeResult['last_time'] = $activeResult['end_time'] - $activeResult['now_time'];
           // var_dump($activeResult);exit();
            //var_dump($activeResult);exit();
            if (is_string($activeResult)) {
                IError::show(403, $activeResult);
            } else {
                $goods_info[$goods_info['promo']] = $activeResult;
            }
        }

        //获得扩展属性
        $tb_attribute_goods = new IQuery('goods_attribute as g');
        $tb_attribute_goods->join = 'left join attribute as a on a.id=g.attribute_id ';
        $tb_attribute_goods->fields = ' a.name,g.attribute_value ';
        $tb_attribute_goods->where = "goods_id='" . $goods_id . "' and attribute_id!=''";
        $goods_info['attribute'] = $tb_attribute_goods->find();

        //购买记录
        $tb_shop = new IQuery('order_goods as og');
        $tb_shop->join = 'left join order as o on o.id=og.order_id';
        $tb_shop->fields = 'count(*) as totalNum';
        $tb_shop->where = 'og.goods_id=' . $goods_id . ' and o.status = 5';
        $shop_info = $tb_shop->find();
        $goods_info['buy_num'] = 0;
        if ($shop_info) {
            $goods_info['buy_num'] = $shop_info[0]['totalNum'];
        }

        //购买前咨询
        $tb_refer = new IModel('refer');
        $refeer_info = $tb_refer->getObj('goods_id=' . $goods_id, 'count(*) as totalNum');
        $goods_info['refer'] = 0;
        if ($refeer_info) {
            $goods_info['refer'] = $refeer_info['totalNum'];
        }

        //网友讨论
        $tb_discussion = new IModel('discussion');
        $discussion_info = $tb_discussion->getObj('goods_id=' . $goods_id, 'count(*) as totalNum');
        $goods_info['discussion'] = 0;
        if ($discussion_info) {
            $goods_info['discussion'] = $discussion_info['totalNum'];
        }

        //获得商品的价格区间
        $tb_product = new IModel('products');
        $product_info = $tb_product->getObj('goods_id=' . $goods_id, 'max(sell_price) as maxSellPrice ,max(market_price) as maxMarketPrice');
        if (isset($product_info['maxSellPrice']) && $product_info['maxSellPrice']) {
            $goods_info['sell_price'] .= "-" . $product_info['maxSellPrice'];
            $goods_info['market_price'] .= "-" . $product_info['maxMarketPrice'];
        }

        //获得会员价
        $countsumInstance = new countsum();
        $goods_info['group_price'] = $countsumInstance->getGroupPrice($goods_id, 'goods');

        //获取商家信息
        if ($goods_info['seller_id']) {
            $sellerDB = new IModel('seller');
            $goods_info['seller'] = $sellerDB->getObj('id = ' . $goods_info['seller_id']);
        }

        //增加浏览次数
        $visit = ISafe::get('visit');
        $checkStr = "#" . $goods_id . "#";
        if ($visit && strpos($visit, $checkStr) !== false) {
        } else {
            $tb_goods->setData(array('visit' => 'visit + 1'));
            $tb_goods->update('id = ' . $goods_id, 'visit');
            $visit = $visit === null ? $checkStr : $visit . $checkStr;
            ISafe::set('visit', $visit);
        }
       // var_dump($goods_info['content'] );
        //这里要给所有img标签加工一下，让客户端懒加载
        //$goods_info['content'] = str_replace('src=', 'class="lazy" data-src=', $goods_info['content']);
        $goods_info['content'] = '<style type="text/css">img{display: block;width: 100%;}</style>' . $goods_info['content'];

       
        Yun::ajaxReturn(0, '请求成功', $goods_info, '');
    }
}